import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ZhiWen Ren
 * @version 1.0
 * @description: LeetCode-139-单词拆分
 * @date 2025/8/12 15:11
 */
public class Solution139 {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> wordDictSet = new HashSet<>(wordDict);
        // dp[i]代表字符串中前i个字符能不能被拆分
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordDictSet.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }

}
